package com.heima.se.sjms.单例.枚举单例;

import com.heima.se.hashmap.Student;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor;

/**
 * @author jack
 * @data 2024 10:04
 */

public class Demo {
    public static void main(String[] args) throws Exception {
        Season spring1 = Season.SPRING;
        Season spring2 = Season.SPRING;

        System.out.println(spring1 == spring2);

        //反射操作不能破坏枚举的单例
        /*Class clazz = spring1.getClass();
        Constructor constructor = clazz.getDeclaredConstructor();
        constructor.setAccessible(true);
        Season spring3 = (Season) constructor.newInstance();
        System.out.println(spring1 == spring3);*/

        //序列化也不能破坏枚举单例
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\IdeaProjects\\heima-leadnews\\heima-leadnews-test\\javaSE-demo\\src\\main\\resources\\stu.txt"));
        oos.writeObject(spring1);          //序列化
        oos.close();

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\IdeaProjects\\heima-leadnews\\heima-leadnews-test\\javaSE-demo\\src\\main\\resources\\stu.txt"));
        Season spring3 = (Season) ois.readObject();      //反序列化
        ois.close();


        System.out.println(spring1 == spring3);
    }
}

enum Season{
    SPRING;             //特点：枚举项就是当前枚举类的对象!  每一个枚举项都是绝对单例的!

    private Season(){};
}
